package com.cssw.fyzb.busi.api.text;


import com.cssw.fyzb.core.model.vo.text.TextKindVo;
import com.cssw.fyzb.core.model.vo.video.VideoVo;
import com.cssw.fyzb.busi.service.text.TextKindService;
import com.cssw.fyzb.core.model.dodb.SysText;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @Auther: zzf
 * @Date: 2020/12/15 14:40
 * @Description:
 */
@Function(description = "文本分类查询", parameters = {
        @Parameter(name = "kindCode", type = Type.String, description = "文本分类编码", required = true),
//        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
//        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class ListTextKind implements Action {

    private final static Logger logger = LoggerFactory.getLogger(ListTextKind.class);

    private static final String KIND_CODE = "kindCode";

    @Autowired
    private SimpleCache simpleCache;

    @Autowired
    private TextKindService testKindService;

    @Override
    public Response execute(Request request) throws Exception {
        try {
            String kindCode = request.getString("kindCode");
            Integer pageNumber = request.getInteger("pageNumber");
            Integer pageSize = request.getInteger("pageSize");
            //获取缓存判断是否以有
            List<TextKindVo> list = simpleCache.get(KIND_CODE + "_" + kindCode);
            //  List<TextKindVo> list = JSON.parseArray(sysText, TextKindVo.class);
            if (list == null) {
                List<SysText> page = testKindService.testKindList(kindCode);
                list = new ArrayList<>();
                if (page != null) {
                    for (SysText v : page) {
                        TextKindVo vo = new TextKindVo();
                        vo.setTextCode(v.getTextCode());
                        vo.setTextName(v.getTextName());
                        vo.setKindCode(v.getKindCode());
                        vo.setContent(v.getContent());
                        vo.setUpdateOperId(v.getUpdateOperId());
                        vo.setUpdateTime(v.getUpdateTime());
                        vo.setCreateTime(v.getCreateTime());
                        vo.setCreateOperId(v.getCreateOperId());
                        vo.setTextUrl(v.getTextUrl());
                        list.add(vo);
                    }
                }
                //有值存缓存
                if (list != null && list.size() != 0) {
                    simpleCache.put(KIND_CODE + "_" + kindCode, (Serializable) list, 7200);
                }
            }

            return Response.success().put("list", list);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }
}
